package frontend.pages.settings.personalprofile;

import frontend.model.settings.SettingsUIModel;
import frontend.pages.admin.base.AbstractManagingForm;
import frontend.components.base.ComponentFactory;
import org.apache.wicket.markup.html.panel.Panel;

public class PersonalProfileForm extends AbstractManagingForm<SettingsUIModel> {

    private static final String RESOURCE_KEY_FIELD_LABEL_NEW_PASSWORD = "pages.personalProfile.form.newPassword";
    private static final String RESOURCE_KEY_FIELD_LABEL_CONFIRMED_NEW_PASSWORD = "pages.personalProfile.form.confirmedNewPassword";
    private static final String RESOURCE_KEY_VALIDATION_NOT_SAME_PASSWORD = "validation.form.personalProfile.notSamePassword";

    public PersonalProfileForm(String id, SettingsUIModel model) {
        super(id, model);
    }

    @Override
    protected void constructFormContent() {
        Panel newPasswordComponent = ComponentFactory.createPasswordFieldComponent("newPassword", RESOURCE_KEY_FIELD_LABEL_NEW_PASSWORD, uiModel);
        Panel confirmedNewPasswordComponent = ComponentFactory.createPasswordFieldComponent("confirmedNewPassword", RESOURCE_KEY_FIELD_LABEL_CONFIRMED_NEW_PASSWORD, uiModel);
        add(newPasswordComponent, confirmedNewPasswordComponent);
    }

    @Override
    protected Class getResponsePage() {
        return PersonalProfilePage.class;
    }

    @Override
    protected void doFormValidation() {
        validateNotEmpty(uiModel.getNewPassword(), RESOURCE_KEY_FIELD_LABEL_NEW_PASSWORD);
        if ((uiModel.getNewPassword() != null && uiModel.getConfirmedNewPassword() != null && !uiModel.getNewPassword().equals(uiModel.getConfirmedNewPassword()))
                || (uiModel.getNewPassword() != null && uiModel.getConfirmedNewPassword() == null)
                || (uiModel.getConfirmedNewPassword() != null && uiModel.getNewPassword() == null)) {
            addFieldValidationError(RESOURCE_KEY_FIELD_LABEL_NEW_PASSWORD, RESOURCE_KEY_VALIDATION_NOT_SAME_PASSWORD);
        }
    }

    protected boolean isDeleteButtonVisible() {
        return false;
    }
}